//
//  ShowDetailVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/8/3.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "ShowDetailVC.h"
#import "ImgCell.h"
#import "ShowDetailHeader.h"
//晒单详情
#define GET_DETAIL_URL @"/api/ApiGoods/getShoppingShareDetail"
@interface ShowDetailVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_data;
    ShowDetailHeader *_header;
    NSInteger count;
}
@end

@implementation ShowDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadCustomNaviBar];
    _data = [NSMutableArray array];
    [self requestForDetail];
    
}
//加载导航条
- (void)loadCustomNaviBar
{
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"晒单详情";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}


//加载表格
- (void)loadTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, WYMHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //加载表头
    _header = [[[NSBundle mainBundle]loadNibNamed:@"ShowDetailHeader" owner:self options:nil] lastObject];
    //调整大小
    if ([_sd_is_virtual isEqualToString:@"1"]) {
        _header.sizeFlag = NO;
        _header.frame = CGRectMake(0, 0, WYMWITH, _header.frame.size.height - 44);
    }else if ([_sd_sd_is_show_express isEqualToString:@"1"]) {
        _header.sizeFlag = YES;
    }else{
        _header.sizeFlag = NO;
        _header.frame = CGRectMake(0, 0, WYMWITH, _header.frame.size.height - 44);
    }
    _header.father = self;
    _tableView.tableHeaderView = _header;
    [self.view addSubview:_tableView];
}
//请求晒单详情
- (void)requestForDetail
{
    if ([WYPublic getUsefullStr:self.model.sd_id].length == 0) {
        return;
    }
    [WYPublic addHudToWindow];
    NSDictionary *params = @{@"sd_id":self.model.sd_id};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,GET_DETAIL_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if ([ret integerValue] == 0) {
            NSDictionary *info = dic[@"data"];
            _sd_code = info[@"q_user_code"];
            _sd_company = info[@"company"];
            _sd_company_code = info[@"company_code"];
            _sd_company_type = info[@"company_mark"];
            NSNumber *num = info[@"go_total"];
            _sd_go_total = [NSString stringWithFormat:@"%ld",(unsigned long)[num integerValue]];
            _sd_id = info[@"sd_id"];
            _sd_is_virtual = info[@"is_virtual"];
            _sd_pdc_end_time = info[@"q_end_time"];
            _sd_pdc_id = info[@"id"];
            _sd_pdc_qishu = info[@"qishu"];
            _sd_pdc_sid = info[@"sid"];
            _sd_pdc_title = info[@"title"];
            _sd_sd_content = info[@"sd_content"];
            _sd_sd_is_show_express = info[@"sd_is_show_express"];
            _sd_sd_photolist_url = info[@"sd_photolist_url"];
            _sd_time = info[@"sd_time"];
            _sd_title = info[@"sd_title"];
            _sd_uid = info[@"uid"];
            _sd_username = info[@"username"];
            _sd_virtual_recharge_type = info[@"virtual_recharge_type"];
            _sd_score = info[@"sd_score"];
            [self loadTableView];
            
            if ([_sd_sd_photolist_url class] != [NSNull class]) {
                if (_sd_sd_photolist_url.count > 0) {
                    for (NSString *str in _sd_sd_photolist_url) {
                        [self loadImgsWith:str];
                    }
                }
            }
        }else{
            [WYPublic removeHudToWindow];
        }
        
    } failedBlock:^{
        [WYPublic removeHudToWindow];
    } needLog:NO];
}
#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < _data.count) {
        UIImage *img = _data[indexPath.row];
        return img.size.height/(img.size.width/(WYMWITH));
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ImgCell alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 400)];
        [cell setRestorationIdentifier:@"cell"];
    }
    if (_data.count > 0) {
        UIImage *img = _data[indexPath.row];
        cell.frame = CGRectMake(0, 0, WYMWITH, img.size.height/(img.size.width/(WYMWITH)));
        [cell fillCellWithImage:img];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//下载图片
- (void)loadImgsWith:(NSString *)imagePath
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    [manager downloadImageWithURL:[NSURL URLWithString:imagePath] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
 
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        [_data addObject:image];
        [_tableView reloadData];
        if (_data.count >= _sd_sd_photolist_url.count) {
            [WYPublic removeHudToWindow];
        }
    }];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [WYPublic removeHudToWindow];
}
@end
